package icu.xblogs.report.interceptor;

import icu.xblogs.report.utils.CookieUtil;
import icu.xblogs.report.utils.JwtUtil;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;

/**
 * @author Frank KONG
 * @version 1.0
 * @date 2020/11/17 9:42
 *
 * 员工登录身份认证
 */
@Component("identityInterceptor")
public class IdentityInterceptor implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws IOException {
        //System.out.println("进入员工拦截器");
        //在此处进行员工登录检测，status/id/token
        HashMap<String, String> hashMap = CookieUtil.getCookie(request, "status", "id", "token");
        System.out.println(hashMap);

        if(hashMap != null && hashMap.size() == 3){
            if("1".equals(hashMap.get("status")) && JwtUtil.getJWT(hashMap.get("id")).equals(hashMap.get("token"))){
                return true;
            }
        }
        //System.out.println("验证失败");
        response.sendRedirect("/index.html");
        return false;
    }
}
